package com.example.komunikator;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.filter.MessageTypeFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.util.StringUtils;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Activity which displays a login screen to the user, offering registration as
 * well.
 */
public class JabbLogIn extends Dialog implements android.view.View.OnClickListener  {
	//	private ArrayList<String> messages = new ArrayList();
	//	private Handler mHandler = new Handler();
	//	public SettingsDialog mDialog;
	//	private EditText mRecipient;
	//	private EditText mSendText;
	//	private ListView mList;
	//	public XMPPConnection connection;
	//	private String someString ="TestowyString";
	private EditText mUserName;
	private EditText mPassword;
	private EditText mChannelName;
	private static final String url = "jdbc:mysql://mysql.agh.edu.pl:3306/rybczyns";
	private static final String user = "rybczyns";
	private static final String pass = "6gtZxjms";
	private LoginActivity JabbL;


	/**
	 * Called with the activity is first created.
	 */

	public JabbLogIn(LoginActivity JabbL) {
		super(JabbL);
		this.JabbL = JabbL;
	}
	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);


		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
				WindowManager.LayoutParams.FLAG_FULLSCREEN);

		setContentView(R.layout.activity_jabb_log_in);
		//SQLConnector sqlconnector = new SQLConnector();
		//		mRecipient = (EditText) this.findViewById(R.id.recipient);
		//		mSendText = (EditText) this.findViewById(R.id.sendText);
		//		mList = (ListView) this.findViewById(R.id.listMessages);

		mUserName = (EditText) this.findViewById(R.id.channel_login);
		mPassword = (EditText) this.findViewById(R.id.channel_password);
		mChannelName = (EditText) this.findViewById(R.id.channel_name);

		setListAdapter();

		// Dialog for getting the xmpp settings


		Button send = (Button) this.findViewById(R.id.jabb_sign_in_button);
		send.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				try {


					Class.forName("com.mysql.jdbc.Driver");  
					Connection con = DriverManager.getConnection(url, user, pass); 
					SQLLogic.getInstance().setConnection(con);
					Statement st = con.createStatement();  
					String query = "SELECT * FROM Users WHERE mail=\""+mUserName.getText()+
							"\"AND password=\""+mPassword.getText()+"\"";
					ResultSet rs = st.executeQuery(query);  
					Log.i("SQLDebug",query);
					if (rs.wasNull()){
						//						Toast.makeText(JabbLogIn.this, "Login or Password are incorrect!", Toast.LENGTH_LONG).show();
						Log.i("SQLDebug","Incorrect Password!");

					}else{
						Log.i("SQLDebug","Password Correct");
						rs=null;
						query = "SELECT DISTINCT name "+
								"FROM ChannelList AS cl  "+
								"JOIN Users AS u ON u.room_id = cl.id "+
								"WHERE mail = \""+mUserName.getText()+"\" "+
								"LIMIT 0 , 30";
						Log.i("SQLDebug",query);
						rs = st.executeQuery(query);

						while (rs.next()){
							Log.i("SQLDebug",rs.getString("name"));
							//									Toast.makeText(JabbLogIn.this, rs.getString("name"), Toast.LENGTH_LONG).show();
						}





					}






				} catch (NullPointerException e) { 
					e.printStackTrace();
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (java.sql.SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} finally{


				} 
				dismiss();



				//				if (connection==null){
				//					Toast.makeText(LoginActivity.this, "Please, go to setup first, or check Your network Connection!", Toast.LENGTH_LONG).show();
				//				}else{
				//
				//					String to = mRecipient.getText().toString();
				//					String text = mSendText.getText().toString();
				//
				//					Log.i("XMPPClient", "Sending text [" + text + "] to [" + to
				//							+ "]");
				//					Message msg = new Message(to, Message.Type.chat);
				//					msg.setBody(text);
				//					connection.sendPacket(msg);
				//					messages.add(connection.getUser() + ":");
				//					messages.add(text);
				//					//
				//					mSendText.setText("");
				//
				//					setListAdapter();
			}


		});
	}



	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{

		//
		//	switch (item.getItemId())
		//	{
		//	case R.id.menu_users:
		//		Intent intent = new Intent(LoginActivity.this, UserList.class);
		//		intent.putExtra("xmppConn", connection.toString());
		//		startActivity(intent);        	
		//		return true;
		//	case R.id.menu_preferences:
		//
		//
		//		mHandler.post(new Runnable() {
		//			public void run() {
		//				mDialog.show();
		//			}
		//		});
		//		return true;
		//
		//	default:
		//		return super.onOptionsItemSelected(item);
		//	}
		return true;
	}   


	/**
	 * Called by Settings dialog when a connection is establised with the XMPP
	 * server
	 * 
	 * @param connection
	 */

	private void setListAdapter() {
		//	ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
		//			R.layout.single_multi_line_list_item, R.id.text1, messages);
		//	// messages);
		//	mList.setAdapter(adapter);


	}
	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub

	}
	private String getText(int id) {
		EditText widget = (EditText) this.findViewById(id);
		return widget.getText().toString();
	}
}
